"""
五子棋主程序入口
"""

import pygame
from gui import MenuGUI, PvPGameGUI, RandomAIGameGUI, GreedyAIGameGUI, MinimaxAIGameGUI


def main():
    """主函数"""
    print("欢迎使用五子棋智能体！")
    print("=" * 50)
    
    # 游戏主循环
    while True:
        # 显示菜单
        menu = MenuGUI()
        selected_mode = menu.run()
        
        if selected_mode is None:
            # 用户关闭菜单窗口
            break
        
        # 根据选择的模式创建游戏界面
        if selected_mode == 'pvp':
            game = PvPGameGUI()
        elif selected_mode == 'random':
            game = RandomAIGameGUI()
            if hasattr(game, 'cancelled') and game.cancelled:
                continue  # 用户取消颜色选择，返回菜单
        elif selected_mode == 'greedy':
            game = GreedyAIGameGUI()
            if hasattr(game, 'cancelled') and game.cancelled:
                continue
        elif selected_mode == 'minimax':
            game = MinimaxAIGameGUI()
            if hasattr(game, 'cancelled') and game.cancelled:
                continue
        else:
            continue
        
        # 运行游戏
        result = game.run()
        
        # 如果返回 'menu'，回到主菜单；否则退出
        if result != 'menu':
            break
    
    pygame.quit()
    print("感谢游玩！")


if __name__ == "__main__":
    main()
